const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    id: 0,
    store: [],
    card: [],
    startshow: false,
    endshow: false,
    minDate: new Date().getTime(),
    week: [
      { name: '周日', checked:0},
      { name: '周一', checked:1},
      { name: '周二', checked:0},
      { name: '周三', checked:0},
      { name: '周四', checked:0},
      { name: '周五', checked:0},
      { name: '周六', checked:0},
    ],
  },
  /**
  * 生命周期函数--监听页面显示
  */
  onLoad: function (event) {
    let that = this;
    if (!app.util().isNull(event.id)) {
      that.setData({
        id: event.id
      })
      that.getCoupon(event.id);
    }
    that.getApi();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    this.setData({
      skeleton: false,
    })
  },
  /**
  * 读取编辑的会员卡
  */
  getCoupon: function (coupon_id) {
    let that = this;
    var week = that.data.week;
    app.api().Get("api/v1/allwin-coupon-editCoupon", {coupon_id:coupon_id }, function (result) {
      for (var i in result.data.weekday){
        week[result.data.weekday[i]].checked = 1;
      }
      that.setData({
        card: result.data,
        card_text: result.data.name,
        checked: result.data.types ? true : false,
        week: week,
      });
    })
  },
  /**
  * 读取我的订单
  */
  getApi: function () {
    let that = this;
    let checked = false;
    app.api().Get("api/v1/allwin-store-manage", function (result) {
      that.setData({
        store: result.data,
      });
    })
  },
  /**
   * 表单双向绑定
   */
  onName: function (event) {
    this.setData({ ['card.name']: event.detail });
  },
  onSize: function (event) {
    this.setData({ ['card.size']: event.detail });
  },
  onTips: function (event) {
    this.setData({ ['card.tips']: event.detail });
  },
  onDiscount:function (event) {
    this.setData({ ['card.discount']: event.detail });
  },
  /**
   * 创建会员卡
   */
  formSubmit: function (e) {
    let that = this;
    var data = e.detail.value;
    var isPost = false;
    var msg = '';
    if (app.util().isNull(data.num)) {
      msg = '发放总量必须填写'
    } else if (app.util().isNull(data.name)) {
      msg = '赠品名称必须填写'
    } else if (app.util().isNull(data.size)) {
      msg = '单张大小必须填写'
    } else if (app.util().isNull(data.price)) {
      msg = '单价必须填写'
    } else if (app.util().isNull(data.howmuch)) {
      msg = '满多少钱可用'
    } else if (app.util().isNull(data.starttime)) {
      msg = '开始时间必须填写'
    } else if (app.util().isNull(data.endtime)) {
      msg = '结束时间必须填写'
    } else if (app.util().isNull(data.tips)) {
      msg = '规则简述必须填写'
    } else {
      isPost = true;
      wx.showLoading({ title: '提交中', mask: true })
      var parms = {
        id: that.data.id,
        num: data.num,
        name: data.name,
        size: data.size,
        price: data.price,
        discount: data.discount,
        howmuch: data.howmuch,
        starttime: data.starttime,
        endtime: data.endtime,
        tips: data.tips,
        types: that.data.checked ? 1 : 0,
        week: that.data.week,
      }
      app.api().Post('api/v1/allwin-coupon-editCoupon?coupon_id='+that.data.id,parms,function(rel){
        wx.navigateBack({
          delta: 1
        })
      })
      wx.hideLoading();
    }
    if (!isPost) {
      wx.showModal({
        content: msg
      })
    }
  },
  //选择周几可以用
  onWeek: function (event) {
    var key = event.currentTarget.dataset.key;
    var week = this.data.week;
    var checked = week[key].checked;
        week[key].checked = week[key].checked == 0 ? 1 : 0;
      this.setData({
        week: week,
      })
  },
  //时间选项
  timesPopup(event) {
    var id = event.target.id;
    if (id == 'start') {
      this.setData({
        startshow: !this.data.startshow
      });
    } else {
      this.setData({
        endshow: !this.data.endshow
      });
    }
  },
  //选择时间
  onTimes(event) {
    var id = event.target.id;
    var times = event.detail;
    var currentDate = app.util().timesToDate(times,'Y-M-D');
    if (id == 'start') {
      this.setData({['card.starttime']: currentDate});
    }else{
      this.setData({['card.endtime']: currentDate});
    }
    this.timesPopup(event);
  },
  //修改赠品类型
  onChange(event) {
    this.setData({
      checked:event.detail
    });
  }
})